﻿namespace Infrastructure.Models
{
    /// <summary>
    /// webapi返回通用类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResult<T>
    {
        public int Code { get; set; } = 200;
        public string Msg { get; set; }
        public T Data { get; set; }

        /// <summary>
        /// 成功返回200状态和数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResult<T> Success(T data)
        {
            return new ApiResult<T> { Code = 200, Data = data };
        }
        // 失败返回400状态
        public static ApiResult<T> Error(string msg, int code = 400)
        {
            return new ApiResult<T> { Code = code, Msg = msg };
        }
    }

    public class ApiResult : ApiResult<object>
    {
        public static ApiResult Success(object data)
        {
            return new ApiResult { Code = 200, Data = data };
        }
        // 失败返回400状态
        public static ApiResult Error(string msg, int code = 400)
        {
            return new ApiResult { Code = code, Msg = msg };
        }
    }
}
